package ar.edu.unq.sandsoft.utils;

import java.lang.reflect.Method;
import java.util.Map;

import ar.edu.unq.sandsoft.constructors.SandFileReader;

public class ValueFetcher {

    private SandFileReader reader;

    public ValueFetcher(final SandFileReader reader) {
        this.reader = reader;
    }

    public Object fetch(Object current) {
        try {
            Map<String, Method> getters = reader.createClassConstructor().prepareGetters();
            String keyProperty = reader.getKeyProperty();
            Object key = getters.get(keyProperty).invoke(current, new Object[] {});
            return key;
        } catch (Exception e) {
            throw new RuntimeException("error fetching value", e);
        }
    }

}
